/********************************************************************************
* File Name:	showhouse.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlQueryModel>
#include "showhouse.h"
#include "ui_showhouse.h"
#include "addnewhouse.h"
#include "deletroom.h"
#include "databaseconnection.h"

showhouse::showhouse(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::showhouse)
{
    ui->setupUi(this);
}

showhouse::~showhouse()
{
    delete ui;
}

void showhouse::on_btndisplay_clicked()
{
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from roomInfo");
        model->setHeaderData(0,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterid"));
        model->setHeaderData(2,Qt::Horizontal,tr("roomtype"));
        model->setHeaderData(3,Qt::Horizontal,tr("location"));
        model->setHeaderData(4,Qt::Horizontal,tr("floor"));
        model->setHeaderData(5,Qt::Horizontal,tr("ratingNum"));
        model->setHeaderData(6,Qt::Horizontal,tr("realNum"));
        model->setHeaderData(7,Qt::Horizontal,tr("area"));
        model->setHeaderData(8,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("air"));
        model->setHeaderData(10,Qt::Horizontal,tr("phone"));
        model->setHeaderData(11,Qt::Horizontal,tr("tv"));
        model->setHeaderData(12,Qt::Horizontal,tr("washer"));
        model->setHeaderData(13,Qt::Horizontal,tr("internet"));
        model->setHeaderData(14,Qt::Horizontal,tr("fridge"));
        model->setHeaderData(15,Qt::Horizontal,tr("remark"));
        ui->tbvHouseInfo->setModel(model);
        //ui->tbvHouseInfo->autoScrollMargin();
        ui->tbvHouseInfo->show();

    }
    databaseconnection::Close();
}

void showhouse::on_btnadd_clicked()
{
    Addnewhouse *ah = new Addnewhouse();
    ah->show();
}

void showhouse::on_btnquit_clicked()
{
    this->close();
}

void showhouse::on_btndel_clicked()
{
  DeletRoom *delR = new DeletRoom();
  delR->show();
}
